package Java7_Concurrency_Cookbook.thread_synchronized_helper.phaser.on.advance;

import java.util.Date;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @author lixiangke
 * @date 2017/11/16
 */
public class Student implements Runnable {
    private Phaser phaser;

    public Student(Phaser phaser) {
        this.phaser = phaser;
    }

    @Override
    public void run() {
        String tName = Thread.currentThread().getName();
        System.out.println(tName + ": Has arrived to do the exam. " + new Date());
        phaser.arriveAndAwaitAdvance();

        System.out.println(tName + ": Is going to do the first exercise. " + new Date());
        doExercise1();
        System.out.println(tName + ": Has done the first exercise. " + new Date());
        phaser.arriveAndAwaitAdvance();

        System.out.println(tName + ": Is going to do the second exercise. " + new Date());
        doExercise2();
        System.out.println(tName + ": Has done the second exercise. " + new Date());
        phaser.arriveAndAwaitAdvance();

        System.out.println(tName + ": Is going to do the third exercise. " + new Date());
        doExercise3();
        System.out.println(tName + ": Has done the third exercise. " + new Date());
        phaser.arriveAndAwaitAdvance();
    }

    private void doExercise1() {
        try {
            long duration = (long) (Math.random() * 10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doExercise2() {
        try {
            long duration = (long) (Math.random() * 10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doExercise3() {
        try {
            long duration = (long) (Math.random() * 10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
